<template>
	<view class="layout">
		<view class="goods-cell-title">
			<h4 class="h4">{{ res.list[0].titleWay[[0]].title }}</h4>
		</view>
		<view class="goods-list">
			<view v-if="
          item.___index != undefined
            ? selected.index == item.___index
            : selected.val == item.type
        " @click="handleClick(item)" class="goods-item" v-for="(item, item_index) in res.list[0].listWay"
				:key="item_index">
				<view class="goods-img">
					<u-image :src="item.img" height="345rpx" mode="aspectFit" width="100%">
						<u-loading slot="loading"></u-loading>
					</u-image>
				</view>
				<view class="goods-desc">
					<view class="goods-title">
						{{ item.title }}
					</view>
					<view class="goods-bottom">
						<view class="goods-price">
							¥<span>{{ $options.filters.goodsFormatPrice(item.price)[0] }}
							</span>.{{ $options.filters.goodsFormatPrice(item.price)[1] }}
						</view>
						<view style="margin-left: auto;font-weight: 400;font-size: 25rpx;color: #C3C3C3;width: 150rpx;text-align: right;
						overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
							{{item.storeName}}
						</view>
					</view>
				</view>
			</view>

			<view v-if="res.list[0].titleWay[selected.index].bindCategory && goodsData.length"
				v-for="(item, index) in goodsData" :key="index" class="goods-item" @click="handleClick(item)">
				<view class="goods-img">
					<u-image :src="item.thumbnail" height="350rpx" mode="aspectFit" width="100%">
						<u-loading slot="loading"></u-loading>
					</u-image>
				</view>
				<view class="goods-desc">
					<view class="goods-title">
						{{ item.goodsName }}
					</view>
					<view class="goods-bottom">
						<view class="goods-price">
							¥<span>{{ $options.filters.goodsFormatPrice(item.price)[0] }}
							</span>.{{ $options.filters.goodsFormatPrice(item.price)[1] }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getGoodsList
	} from "@/api/goods.js";
	export default {
		title: "商品分类以及商品",
		data() {
			return {
				selected: {
					index: 0,
					val: "",
				},
				params: {
					pageNumber: 1,
					pageSize: 100,
					categoryId: "",
				},
				goodsData: [], //商品循环内容
				goodsResult: "", //es总返回内容
			};
		},
		props: ["res"],
		watch: {
			res: {
				handler(val) {
					// 监听父级的值 如果有值将值赋给selected
					if (val) {
						console.log(val)
						// 如果第一个标签页绑定为商品
						this.selected.val = this.res.list[0].listWay[0] ? this.res.list[0].listWay[0].type : '';
						// 如果第一个标签为绑定为分类
						this.res.list[0].titleWay[0].bindCategory ? this.initGoods(this.res.list[0].titleWay[0]) : ''
					}
				},
				immediate: true,
			},
		},
		mounted() {

		},
		destroyed() {},
		methods: {
			handleClick(item) {
				uni.navigateTo({
					url: `/pages/product/goods?goodsId=${item.id}`,
				});
			},
			closeGoods(val, index) {
				this.res.list[0].listWay.splice(index, 1);
			},
			handleClickTitle(val, index) {
				this.selected.index = index;
				this.selected.val = val.title;
				if (val.bindCategory) {
					this.params.pageNumber = 1
					this.goodsData = []
					this.initGoods(val);
				}
			},
		},
	};
</script>
<style lang="scss" scoped>
	$w_94: 100%;

	.layout {
		// padding: 8px 0;
		background: #F4F4F4;
		// width: 710rpx;
		// margin: 0 auto;
	}

	.selected-title {
		>h4 {
			font-size: 30rpx;
			color: #000 !important;
		}

		>view {
			font-weight: bold;
			color: $main-color !important;
		}
	}

	.goods-cell-title {
		background: #F4F4F4;
		padding: 20rpx;
		transition: 0.35s;
		display: flex;

		h4 {
			font-size: 32rpx;
		}
	}

	.goods-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
	}

	.goods-item {
		// width: 50%;
		width: 345rpx;
		margin-bottom: 10px;
		border-radius: 0.4em;
		overflow: hidden;
		margin-left: 20rpx;
	}

	.goods-img {
		position: relative;
		margin: 0 auto;
		// width: 158px;
		width: $w_94;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		overflow: hidden;

		>img {
			width: 100%;
			height: 100%;
		}
	}

	.goods-desc {
		border-bottom-left-radius: 20rpx;
		border-bottom-right-radius: 20rpx;
		width: $w_94;
		background: #fff;
		// padding: 8rpx 0 8rpx 8rpx;
		padding: 8rpx;
		margin: 0 auto;

		>.goods-title {
			font-size: 24rpx;
			height: 67rpx;
			display: -webkit-box;
			font-weight: 500;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}

		>.goods-bottom {
			display: flex;
			font-weight: bold;
			align-items: center;

			>.goods-price {
				line-height: 2;
				color: $main-color;

				>span {
					font-size: 42rpx;
				}
			}
		}
	}
</style>